package com.ningxia.water.controller.auth;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.ningxia.water.entity.SysBackendApiTable;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.SysBackendApiTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (SysBackendApiTable)表控制层
 */
@RestController
@RequestMapping("/api")
@Api(tags = {"权限控制-后台API管理"})
public class SysBackendApiTableController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SysBackendApiTableService sysBackendApiTableService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/getinfos")
    @ApiOperation(value = "所有数据")
    public Result selectAll() {
        return ResultResponse.getSuccessResult(this.sysBackendApiTableService.getAllApiList());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getinfo/{id}")
    @ApiOperation(value = "通过id查询单条数据")
    public Result selectOne(@PathVariable Serializable id) {
        return ResultResponse.getSuccessResult(this.sysBackendApiTableService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysBackendApiTable 实体对象
     * @return 新增结果
     */
    @PostMapping("/insertapi")
    @ApiOperation(value = "增加api")
    public Result insert(@RequestBody SysBackendApiTable sysBackendApiTable) {
        //如果没有父级，设置为url=none
        if (sysBackendApiTable.getPid() == null) {
            sysBackendApiTable.setBackendApiUrl("none");
        }
        boolean isSave = sysBackendApiTableService.save(sysBackendApiTable);
        if(isSave){
            return ResultResponse.getSuccessResult("添加成功");
        }else {
            return ResultResponse.getFailResult("添加失败");
        }
    }

    /**
     * 修改数据
     *
     * @param sysBackendApiTable 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateapi")
    @ApiOperation(value = "修改api")
    public Result update(@RequestBody SysBackendApiTable sysBackendApiTable) {
        boolean isUpdate = this.sysBackendApiTableService.updateById(sysBackendApiTable);
        if (isUpdate){
            return ResultResponse.getSuccessResult("更新成功");
        }else {
            return ResultResponse.getFailResult("更新失败");
        }


    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteapi")
    @ApiOperation(value = "删除api")
    public Result delete(@RequestParam("idList") List<String> idList) {
        boolean isDelete = this.sysBackendApiTableService.removeByIds(idList);
        if (isDelete){
            return ResultResponse.getSuccessResult("删除成功");
        }else {
            return ResultResponse.getFailResult("删除失败");
        }

    }
}
